{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "tags": [
          "all"
        ]
      },
      "source": [
        "# Results of {{name}}\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "tags": [
          "all",
          "data"
        ]
      },
      "outputs": [],
      "source": [
        "data = {{data}}"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "tags": [
          "all"
        ]
      },
      "outputs": [],
      "source": [
        "import pandas as pd\n",
        "import matplotlib.pyplot as plt\n",
        "import seaborn as sns\n",
        "\n",
        "df = pd.DataFrame(data)\n",
        "\n",
        "var_names = {{var_names}}\n",
        "x_vars = {{x_vars}}\n",
        "y_vars = {{y_vars}}\n",
        "\n",
        "display(df)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "tags": [
          "all"
        ]
      },
      "outputs": [],
      "source": [
        "def get_name(var):\n",
        "    return var_names[var] if var in var_names else var"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "tags": [
          "bar"
        ]
      },
      "outputs": [],
      "source": [
        "# BAR\n",
        "\n",
        "for y_var in y_vars:\n",
        "    values = df.groupby({{x_vars}})[y_var]\n",
        "\n",
        "    {%- if x_vars|length > 1 %}\n",
        "    values.mean().unstack().plot.bar(yerr=values.std().unstack())\n",
        "    {% else %}\n",
        "    values.mean().plot.bar(yerr=values.std())\n",
        "    {% endif %}\n",
        "\n",
        "    plt.xlabel('{{x_names[0]}}')\n",
        "    plt.ylabel(get_name(y_var))\n",
        "    plt.legend(title='{{x_names[1]}}')\n",
        "    plt.title('{{name}}')\n",
        "    plt.grid(alpha=0.3)\n",
        "\n",
        "    # plt.savefig(f'{{file_name}}_{y_var}.pdf')\n",
        "    plt.show()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "tags": [
          "barh"
        ]
      },
      "outputs": [],
      "source": [
        "# BARH (HORIZONTAL BAR)\n",
        "\n",
        "for y_var in y_vars:\n",
        "    values = df.groupby({{x_vars}})[y_var]\n",
        "\n",
        "    {%- if x_vars|length > 1 %}\n",
        "    values.mean().unstack().plot.barh(xerr=values.std().unstack())\n",
        "    {% else %}\n",
        "    values.mean().plot.barh(xerr=values.std())\n",
        "    {% endif %}\n",
        "\n",
        "    plt.xlabel(get_name(y_var))\n",
        "    plt.ylabel('{{x_names[0]}}')\n",
        "    plt.legend(title='{{x_names[1]}}')\n",
        "    plt.title('{{name}}')\n",
        "    plt.grid(alpha=0.3)\n",
        "\n",
        "    # plt.savefig(f'{{file_name}}_{y_var}.pdf')\n",
        "    plt.show()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "tags": [
          "line"
        ]
      },
      "outputs": [],
      "source": [
        "# LINE(S)\n",
        "\n",
        "for y_var in y_vars:\n",
        "    values = df.groupby({{x_vars}})[y_var]\n",
        "\n",
        "    {%- if x_vars|length > 1 %}\n",
        "    values.mean().unstack().plot.line(yerr=values.std().unstack())\n",
        "    {% else %}\n",
        "    values.mean().plot.line(yerr=values.std())\n",
        "    {% endif %}\n",
        "\n",
        "    plt.xlabel('{{x_names[0]}}')\n",
        "    plt.ylabel(get_name(y_var))\n",
        "    plt.legend(title='{{x_names[1]}}')\n",
        "    plt.title('{{name}}')\n",
        "    plt.grid(alpha=0.3)\n",
        "\n",
        "    # plt.savefig(f'{{file_name}}_{y_var}.pdf')\n",
        "    plt.show()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "tags": [
          "boxplot"
        ]
      },
      "outputs": [],
      "source": [
        "# BOXPLOT\n",
        "\n",
        "{%- if x_vars|length == 1 %}\n",
        "\n",
        "for y_var in y_vars:\n",
        "    sns.boxplot(x='{{x_vars[0]}}', y=y_var, data=df)\n",
        "\n",
        "    plt.xlabel('{{x_names[0]}}')\n",
        "    plt.ylabel(get_name(y_var))\n",
        "    plt.title('{{name}}')\n",
        "    plt.grid(alpha=0.3)\n",
        "\n",
        "    # plt.savefig(f'{{file_name}}_{y_var}.pdf')\n",
        "    plt.show()\n",
        "\n",
        "{%- else %}\n",
        "\n",
        "# select the variable with the fewest unique values for the legend\n",
        "legend_x_var = df[x_vars].nunique().idxmin()\n",
        "\n",
        "# for each y_var, plot all x_var combinations (except the one used for the legend)\n",
        "for yv in y_vars:\n",
        "    for xv in [var for var in x_vars if var != legend_x_var]:\n",
        "        sns.boxplot(x=xv, y=yv, hue=legend_x_var, data=df)\n",
        "        plt.xlabel(get_name(xv))\n",
        "        plt.ylabel(get_name(yv))\n",
        "        plt.legend(title=get_name(legend_x_var))\n",
        "        plt.title('{{name}}')\n",
        "        plt.grid(alpha=0.3)\n",
        "\n",
        "        # plt.savefig(f'{{file_name}}_{y_var}.pdf')\n",
        "        plt.show()\n",
        "\n",
        "{%- endif %}"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "tags": [
          "cdf"
        ]
      },
      "outputs": [],
      "source": [
        "# CDF\n",
        "\n",
        "{%- if x_vars|length == 1 %}\n",
        "\n",
        "for y_var in y_vars:\n",
        "    for x_var, x_var_values in df.groupby({{x_vars}}):\n",
        "        sns.kdeplot(x_var_values[y_var], cumulative=True, label=x_var)\n",
        "\n",
        "    plt.xlabel(get_name(y_var))\n",
        "    plt.ylabel(f'Cumulative distribution of {get_name(y_var)}')\n",
        "    plt.legend(title=get_name(x_var))\n",
        "    plt.title('{{name}}')\n",
        "    plt.grid(alpha=0.3)\n",
        "\n",
        "    # plt.savefig(f'{{file_name}}_{y_var}.pdf')\n",
        "    plt.show()\n",
        "\n",
        "{%- else %}\n",
        "\n",
        "# select the variable with the fewest unique values for the legend\n",
        "legend_x_var = df[x_vars].nunique().idxmin()\n",
        "\n",
        "# for each y_var, plot all x_var combinations (except the one used for the legend)\n",
        "for yv in y_vars:\n",
        "    for xv in [var for var in x_vars if var != legend_x_var]:\n",
        "        for x_var, x_var_values in df.groupby({{x_vars}}):\n",
        "            sns.kdeplot(x_var_values[yv], cumulative=True, label=x_var)\n",
        "\n",
        "        plt.xlabel(get_name(yv))\n",
        "        plt.ylabel(f'Cumulative distribution of {get_name(yv)}')\n",
        "        plt.legend(title=get_name(legend_x_var))\n",
        "        plt.title('{{name}}')\n",
        "        plt.grid(alpha=0.3)\n",
        "\n",
        "        # plt.savefig(f'{{file_name}}_{y_var}.pdf')\n",
        "        plt.show()\n",
        "\n",
        "{%- endif %}"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "tags": [
          "heatmap"
        ]
      },
      "outputs": [],
      "source": [
        "# HEATMAP\n",
        "\n",
        "{%- if x_vars|length == 2 %}\n",
        "\n",
        "for y_var in y_vars:\n",
        "    sns.heatmap(df.pivot_table(index=x_vars[0], columns=x_vars[1], values=y_var))\n",
        "\n",
        "    plt.xlabel(get_name(x_vars[1]))\n",
        "    plt.ylabel(get_name(x_vars[0]))\n",
        "    plt.title('{{name}}')\n",
        "    plt.grid(alpha=0.3)\n",
        "\n",
        "    # plt.savefig(f'{{file_name}}_{y_var}.pdf')\n",
        "    plt.show()\n",
        "\n",
        "{%- else %}\n",
        "\n",
        "# select the variable with the fewest unique values for the legend\n",
        "legend_x_var = df[x_vars].nunique().idxmin()\n",
        "\n",
        "# for each y_var, plot all x_var combinations (except the one used for the legend)\n",
        "for yv in y_vars:\n",
        "    for xv in [var for var in x_vars if var != legend_x_var]:\n",
        "        sns.heatmap(df.pivot_table(index=xv, columns=legend_x_var, values=yv))\n",
        "\n",
        "        plt.xlabel(get_name(legend_x_var))\n",
        "        plt.ylabel(get_name(xv))\n",
        "        plt.title('{{name}}')\n",
        "        plt.grid(alpha=0.3)\n",
        "\n",
        "        # plt.savefig(f'{{file_name}}_{y_var}.pdf')\n",
        "        plt.show()\n",
        "\n",
        "{%- endif %}"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.12.2"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}
